package com.qst.system.controller;

import com.qst.common.core.controller.BaseController;
import com.qst.common.core.domain.AjaxResult;
import com.qst.common.core.page.TableDataInfo;
import com.qst.system.domain.vo.BankcardVo;
import com.qst.system.service.IBankcardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 银行卡管理控制器
 * 对应前端Bankcard.vue页面的接口请求
 */
@RestController
@RequestMapping("/system/bankcard")
public class BankcardController extends BaseController {

    @Autowired
    private IBankcardService bankcardService;

    /**
     * 查询银行卡列表
     * 对应前端getList()方法的请求
     */
    @PreAuthorize("@ss.hasPermi('system:bankcard')")
    @GetMapping("/list")
    public TableDataInfo selectBankcardList(BankcardVo bankcard) {
        startPage(); // 开启分页
        List<BankcardVo> bankcardVos = bankcardService.selectBankcardList(bankcard);
        return getDataTable(bankcardVos); // 封装分页数据返回
    }

    /**
     * 根据ID查询银行卡详情
     * 对应前端handleUpdate()方法中获取详情的请求
     */
    @PreAuthorize("@ss.hasPermi('system:bankcard:edit')")
    @GetMapping("/{id}")
    public AjaxResult selectBankcardById(@PathVariable Long id) {
        return AjaxResult.success(bankcardService.selectBankcardById(id));
    }

    /**
     * 新增银行卡
     * 对应前端submitForm()方法中的新增操作
     */
    @PreAuthorize("@ss.hasPermi('system:bankcard:add')")
    @PostMapping
    public AjaxResult add(@RequestBody BankcardVo bankcard) {
        return AjaxResult.success(bankcardService.insertBankcard(bankcard));
    }

    /**
     * 修改银行卡
     * 对应前端submitForm()方法中的修改操作
     */
    @PreAuthorize("@ss.hasPermi('system:bankcard:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody BankcardVo bankcard) {
        return AjaxResult.success(bankcardService.updateBankcard(bankcard));
    }

    /**
     * 删除银行卡（支持单个和批量删除）
     * 对应前端handleDelete()方法的请求
     */
    @PreAuthorize("@ss.hasPermi('system:bankcard:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String ids) {
        try {
            // 处理单个ID情况
            if (!ids.contains(",")) {
                return toAjax(bankcardService.deleteBankcardById(Long.parseLong(ids)));
            }

            // 处理批量ID情况
            String[] idStrs = ids.split(",");
            Long[] idsArr = new Long[idStrs.length];
            for (int i = 0; i < idStrs.length; i++) {
                idsArr[i] = Long.parseLong(idStrs[i].trim());
            }
            return toAjax(bankcardService.deleteBankcardByIds(idsArr));
        } catch (NumberFormatException e) {
            return AjaxResult.error("删除失败，ID格式不正确");
        } catch (Exception e) {
            return AjaxResult.error("删除失败：" + e.getMessage());
        }
    }

    /**
     * 获取公司名称列表
     * 对应前端getCompanyName()方法的请求
     */
    @PreAuthorize("@ss.hasPermi('system:bankcard:list')")
    @GetMapping("/comName")
    public AjaxResult getCompanyName() {
        // 获取所有公司名称列表
        List<BankcardVo> companyList = bankcardService.selectCompanyNameList();
        return AjaxResult.success(companyList);
    }
}